package at.kokoni.Koko_Ni.util;

import java.net.MalformedURLException;
import java.net.URL;
import java.util.LinkedList;
import java.util.List;

public class UrlBuilder {
	private String baseUrl;
	private List<String> params = new LinkedList<String>();

	public UrlBuilder(String baseUrl) {
		this.baseUrl = baseUrl;
	}

	public UrlBuilder(String baseUrl, String... params) {
		this.baseUrl = baseUrl;
		addParams(params);
	}

	public UrlBuilder addParams(String... params) {
		for (String param : params) {
			this.params.add(param);
		}
		return this;
	}

	public URL getUrl() {
		try {
			return new URL(this.toString());
		} catch (MalformedURLException e) {
			return null;
		}
	}

	@Override
	public String toString() {
		if (params != null && !params.isEmpty()) {
			StringBuilder result = new StringBuilder(this.baseUrl + "?");
			for (String param : this.params) {
				result.append(param).append("&");
			}
			return result.toString().substring(0, result.toString().length() - 2);
		} else {
			return baseUrl;
		}
	}
}
